package com.example.demo6.ui.detail;

import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.SQLite.MyDatabase;

public class losedetailFragment extends Fragment {

    private TextView showLostName, showLoster, ShowLostPlace, showLostTime, showLostPhone, showLostDetail;
    private Button LostDetailReturn;

    LostGoods lostGoods;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_losedetail, container, false);

        // 初始化NavController
        showLostName = view.findViewById(R.id.showLostName);
        showLoster = view.findViewById(R.id.showLoster);
        ShowLostPlace = view.findViewById(R.id.ShowLostPlace);
        showLostTime = view.findViewById(R.id.showLostTime);
        showLostPhone = view.findViewById(R.id.showLostPhone);
        showLostDetail = view.findViewById(R.id.showLostDetail);
        // 从Bundle中获取goodsId
        int goodsId = getArguments() != null ? getArguments().getInt("goodsId") : -1;
        if (goodsId != -1) {
            new SelectLostGoodsTask(goodsId).execute();
        }

        // 设置返回按钮的点击监听器
        Button LostDetailReturn = view.findViewById(R.id.LostDetailReturn);
        LostDetailReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController= Navigation.findNavController((Activity) getContext(),R.id.nav_host_fragment_activity_main);
                navController.popBackStack();
            }
        });

        return view;
    }

    @SuppressLint("StaticFieldLeak")
    private class SelectLostGoodsTask extends AsyncTask<Void, Void, Void>
    {
        int goodsId;
        public SelectLostGoodsTask(int goodsId)
        {
            this.goodsId=goodsId ;
        }
        //不能在这里更新UI,否则有异常
        @Override
        protected Void doInBackground(Void... arg0)
        {
            lostGoods= MyDatabase.getInstance(getContext()).lostGoodsDao().getLostGoodsByGoodsId(goodsId);
            return null;
        }
        //onPostExecute用于doInBackground执行完后，可以更新界面UI。
        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            // 绑定数据到视图
            bindDataToView(lostGoods);

        }
    }

    private void bindDataToView(LostGoods lostGoods) {
        showLostName.setText(lostGoods.lostGoods);
        showLoster.setText(lostGoods.loser);
        ShowLostPlace.setText(lostGoods.lostPlace);
        showLostTime.setText(lostGoods.lostTime);
        showLostPhone.setText(lostGoods.loserPhone);
        showLostDetail.setText(lostGoods.lostDetail);
    }
}